//
//  GGFuntionView.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/4/4.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "GGFuntionView.h"
#import "GGAddtion.h"
#import "GGZFBFuntionFlowLayout.h"
#import "GGFuntionCell.h"
#import "ZFBFunctionModel.h"
#import "GGFuntionCycleViewCell.h"
#import "GGHomeTopView.h"
#import "GGTopReusableView.h"
static NSString *cellID = @"cellID";
static NSString *funtionCellID = @"funtionCellID";
static NSString *headerID = @"headerID";
@interface GGFuntionView ()<UICollectionViewDelegate,UICollectionViewDataSource,topReusableViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray <ZFBFunctionModel *>*dataList;
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation GGFuntionView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self loadData];
        [self setUI];
    }
    return self;
}
- (void)setUI{
    
    GGZFBFuntionFlowLayout *layout = [[GGZFBFuntionFlowLayout alloc]init];
//    layout.headerReferenceSize = CGSizeMake(CGRectGetWidth(self.frame), 100 + 120);
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)) collectionViewLayout:layout];
    self.collectionView = collectionView;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self addSubview:collectionView];
    [self.collectionView registerClass:[GGFuntionCycleViewCell class] forCellWithReuseIdentifier:cellID];
    [self.collectionView registerClass:[GGFuntionCell class] forCellWithReuseIdentifier:funtionCellID];
    [self.collectionView registerClass:[GGTopReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    if(section == 0)
//        return 1;
    return self.dataList.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    if(indexPath.section == 0){
//        GGFuntionCycleViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
//           return cell;
//    }
    GGFuntionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:funtionCellID forIndexPath:indexPath];
    cell.model = self.dataList[indexPath.item];
    return cell;
}
#pragma mark <UICollectionViewDelegateFlowLayout>
- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    if(indexPath.section == 0){
//        return CGSizeMake(CGRectGetWidth(self.frame), 120);
//    }
    return CGSizeMake((CGRectGetWidth(self.frame) - 3) / 4, (CGRectGetWidth(self.frame) - 3) / 4);
}
#pragma mark - 设置collectionView每组组头的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if(section == 0)
        return CGSizeMake(CGRectGetWidth(self.frame), 220);
    return CGSizeMake(CGRectGetWidth(self.frame), 10);
}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
//    if(section == 0)
//          return CGSizeMake(CGRectGetWidth(self.frame), 220);
//      return CGSizeMake(CGRectGetWidth(self.frame), 10);
//}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
//    if(indexPath.section == 0){
//        GGTopReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID forIndexPath:indexPath];
//        return headerView;
//    }
    GGTopReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID forIndexPath:indexPath];
    headerView.delegate = self;
    return headerView;
}
- (void)btnWithType:(NSInteger)type{
    if([self.delegate respondsToSelector:@selector(funtionViewWithType:)]){
        [self.delegate funtionViewWithType:type];
    }
}
- (void)loadData{
    self.dataList = [NSArray gg_arrayModelWithPlistName:@"FunctionsList.plist" withClassName:@"ZFBFunctionModel"].mutableCopy;
}
- (NSMutableArray <ZFBFunctionModel *>*)dataList{
    if(!_dataList){
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}

@end
